package com.homeWork;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class day1 {
    @Test
    //通过api接口发送通知类短信
    void shortMsg() throws IOException {
        URL url = new URL("https://itdage.com/kkb/kkbsms?key=xzk&number=13700767792&code=14zz");
        URLConnection conn = url.openConnection();
        InputStream inputStream = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String text = br.readLine();
        System.out.println(text);
        br.close();
    }

    /**
     * 使用图灵机器人进行各种小对话：如查询你天气、讲段子
     * @throws IOException
     */
    @Test
    void TuringRobot() throws IOException {
        URL url = new URL("https://api.jisuapi.com/iqa/query?appkey=62958a3a6ef3c56d&question=罗山明天的天气");
        URLConnection conn = url.openConnection();
        InputStream inputStream = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String text = br.readLine();

                //string 转为 map  通过Gson依赖包
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Gson gson = new Gson();
            map = gson.fromJson(text, new TypeToken<Map<String, Object>>() {
            }.getType());
        } catch (JsonSyntaxException e) {
            throw new RuntimeException(e);
        }
        System.out.println("map的值为:"+map);
        Map<String, Object>  result = (Map<String, Object>) map.get("result");
        System.out.println(result);
        System.out.println("图灵机器人的回复："+result.get("content"));
//        System.out.println(text);

        br.close();
    }
}
